package com.blockchain.coupon.service.impl;

import com.blockchain.coupon.dao.MerchantMapper;
import com.blockchain.coupon.entity.*;
import com.blockchain.coupon.service.MerchantService;
import com.blockchain.coupon.sha3.Sha3;
import com.blockchain.coupon.util.Encryption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by flynn on 17-3-8.
 */
@Service(value = "merchantService")
public class MerchantServiceImpl implements MerchantService{

    @Autowired
    private MerchantMapper merchantMapper;
    /**
     * @param merchant
     * @return resultCode
     *              1; 成功
     *              2: 用户名已存在
     *              3: 注册失败
     */
    @Override
    public String merchantRegister(Merchant merchant) {

        String resultCode;
        // 判断用户是否已存在
        if(this.merchantMapper.queryMerchantByAccount(merchant.getMerchantAccount()) != null) {
            resultCode = "2";
        }else {
            // 设置属性
            merchant.setMerchantId(UUID.randomUUID().toString());
            merchant.setMerchantSalt(Encryption.createSalt());
            merchant.setMerchantPassword(Sha3.sha3(merchant.getMerchantPassword() + merchant.getMerchantSalt()));
            merchant.setMerchantPublicKey("testPublicKey");
            merchant.setMerchantRegisterStatus("0");
            System.out.println(merchant);
            if(this.merchantMapper.insertMerchant(merchant) == 1) {
                resultCode = "1";
            }else {
                resultCode  = "3";
            }
        }
        return resultCode;
    }

    @Override
    public String merchantLogin(String merchantAccount, String merchantPassword) {
        String resultCode;
        Merchant merchant = this.merchantMapper.queryMerchantByAccount(merchantAccount);
        // 没有此用户
        if(merchant == null) {
            resultCode = "2";
        }else {
            if(merchant.getMerchantPassword().equals(Sha3.sha3(merchantPassword + merchant.getMerchantSalt()))) {
                resultCode = "1";
            }else {
                resultCode = "3";
            }
        }
        return resultCode;
    }

    @Override
    public String merchantInfoModify(String merchantId, String merchantName, String merchantDescription) {
        String resultCode;
        if(this.merchantMapper.updateMerchantInfo(merchantId, merchantName, merchantDescription) == 1) {
            resultCode = "1";
        }else {
            resultCode = "2";
        }
        return resultCode;
    }

    @Override
    public String merchantPasswordModify(String merchantId, String merchantPassword, String merchantNewPassword) {
        String resultCode;
        Merchant merchant = this.merchantMapper.queryMerchantByMerchantId(merchantId);
        System.out.println(merchant);
        if(merchant.getMerchantPassword().equals(Sha3.sha3(merchantPassword+merchant.getMerchantSalt()))) {
            merchantNewPassword = Sha3.sha3(merchantNewPassword + merchant.getMerchantSalt());
            if(this.merchantMapper.updateMerchantPassword(merchantId, merchantNewPassword) == 1) {
                resultCode = "1";
            }else {
                resultCode = "3";
            }
        }else {
            resultCode = "2";
        }
        return resultCode;
    }

    @Override
    public List<BankCard> merchantBankCardInfoDisplay(String merchantId) {
        return this.merchantMapper.queryBankCardByMerchantId(merchantId);
    }

    @Override
    public Map<String, Object> merchantClearingTicketApplyDisplay(String merchantId, String startTime, String endTime) {
        Map<String, Object> map = new HashMap<>();
        Merchant merchant = this.merchantMapper.queryMerchantByMerchantId(merchantId);
        map.put("merchantAccountBalance", merchant.getMerchantAccountBalance());
        map.put("clearingTicketTotalBalance", merchant.getClearingTicketTotalBalance());
        List<ClearingTicketOperationLog> applyList = this.merchantMapper.queryClearingTicketOperationLogListByMerchantId(merchantId, startTime, endTime);
        map.put("applyList", applyList);
        return map;
    }

    @Override
    public String merchantApplyClearingTicket(String merchantId, String clearingTicketApplyAmount) {
        String resultCode;
        ClearingTicket clearingTicket = new ClearingTicket();
        clearingTicket.setClearingTicketId(UUID.randomUUID().toString());
        clearingTicket.setMerchantId(merchantId);
        clearingTicket.setClearingTicketAmount(clearingTicketApplyAmount);
        clearingTicket.setApplyDate((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
        clearingTicket.setClearingTicketStatus("1");
        if(this.merchantMapper.insertClearingTicket(clearingTicket) == 1) {
            resultCode = "1";
        }else {
            resultCode = "2";
        }
        return resultCode;
    }

    @Override
    public List<ClearingTicketOperationLog> merchantApplyRecordFiltrate(String merchantId, String startTime, String endTime) {
        return this.merchantMapper.queryClearingTicketOperationLog(merchantId, startTime, endTime);
    }

    @Override
    public String merchantIssueCouponDisplay(String merchantId) {
        return this.merchantMapper.queryMerchantByMerchantId(merchantId).getClearingTicketTotalBalance();
    }

    @Override
    public String merchantIssueCoupon(String merchantId, CouponRule couponRule) {

        String resultCode;
        couponRule.setCouponRuleId(UUID.randomUUID().toString());
        couponRule.setMerchantId(merchantId);
        // 插入结算券规则表
        if (this.merchantMapper.insertCouponRule(couponRule) == 1) {
            resultCode = "1";
        }else {
            resultCode = "2";
        }
        return resultCode;
    }

    @Override
    public CouponRule merchantIssueCouponManageDisplay(String merchantId) {

        return this.merchantMapper.queryCouponRuleByMerchantId(merchantId);
    }

    @Override
    public String merchantStopIssueCoupon(String couponRuleId) {
        return null;
    }

    @Override
    public Map<String, Object> merchantIssueStatusDisplay(String couponRuleId) {
        return null;
    }

    @Override
    public List<CouponOperationLog> merchantStopIssueStatusFiltrate(String merchantId, String startTime, String endTime) {
        return null;
    }
/*
    @Override
    public boolean merchantInfoSetting(String merchantName, String merchantDescription) {
        return false;
    }

    @Override
    public int merchantPasswordSetting(String merchantPassword, String merchantPasswordNew) {
        return 0;
    }

    @Override
    public BankCard queryMerchantBankCard(String merchantId) {
        return null;
    }

    @Override
    public boolean applyClearingTicket(int clearingTicketAmount) {
        return false;
    }

    @Override
    public boolean issueCouponApplication(CouponRule couponRule) {
        return false;
    }

    @Override
    public boolean stopCouponIssuement(String couponId) {
        return false;
    }

    @Override
    public List<CouponIssueInfo> queryCouponStatusHistory(String couponId) {
        return null;
    }

    @Override
    public boolean capitalClearing(String merchantId) {
        return false;
    }

    @Override
    public List<CapitalFlow> queryCapitalFlow(String merchantId) {
        return null;
    }
    */
}
